<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>模拟重力运动</title>
    <style>
        div {
            position: absolute;
            left: 0px;
            top: 0px;
            width: 100px;
            height: 100px;
            background: orange;
            border-radius: 50px;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        var oDiv = document.getElementsByTagName('div')[0];
        oDiv.onclick = function () {
            startMove(this);
        }
        function startMove(obj) {
            clearInterval(obj.timer);
            var iSpeedX = 6;//加速度X
            var iSpeedY = 8;//加速度Y
            var g = 6;
            obj.timer = setInterval(function () {
                iSpeedY += g ;
                var newLeft = obj.offsetLeft + iSpeedX;
                var newTop = obj.offsetTop + iSpeedY;
                if (newTop >= document.documentElement.clientHeight - obj.offsetHeight) {
                    iSpeedY *= -1;
                    iSpeedX *= 0.8;
                    iSpeedY *= 0.8;
                    newTop = document.documentElement.clientHeight - obj.offsetHeight;
                }
                if (newTop <= 0) {
                    iSpeedY *= -1;
                    iSpeedX *= 0.8;
                    iSpeedY *= 0.8;
                }

                if (newLeft >= document.documentElement.clientWidth - obj.offsetWidth) {
                    iSpeedX *= -1;
                    iSpeedX *= 0.8;
                    iSpeedY *= 0.8;
                   newLeft = document.documentElement.clientWidth - obj.offsetWidth;
                }
                if (newLeft <= 0) {
                    iSpeedX *= -1;
                    iSpeedX *= 0.8;
                    iSpeedY *= 0.8;
                   
                }
                obj.style.left = newLeft + 'px';
                // console.log(obj.style.left);
                obj.style.top = newTop + 'px';
                // console.log(obj.style.top);
            }, 30);//每隔30ms执行函数一次
        }
    </script>
</body>

</html>